package cn.arnold.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Arnold-Xiang
 * @Info 最长无重复的子序列
 */
public class LengthOfLongestSubstring {
    public static void main(String[] args) {
        String str = "pwkwee";
        System.out.println(lengthOfLongestSubstring(str));
    }

    public static int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        int max = 0;
        int i = 0, j = 0;
        for (; j < s.length(); j++) {
            char ch = s.charAt(j);
            if (map.containsKey(ch)) {
                i = Math.max(i, map.get(ch));
            }
            map.put(ch, j + 1);
            max = Math.max(max, j - i + 1);
        }
        return max;
    }
}
